from lxml import etree
import requests
import json
from openpyxl import Workbook

wb = Workbook()
ws = wb.active
ws.append(["英雄","故事"])

headers = {
    "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"
}
url = "https://pvp.qq.com/web201605/js/herolist.json"
detail_url = "https://pvp.qq.com/web201605/herodetail/{}.shtml"

with open("./data/herolist.json",'r',encoding="utf-8")as f:
    hero_list = json.loads(f.read())

for hero in hero_list:
    hero_name = hero['cname']
    ename = hero['ename']
    detail_url_full = detail_url.format(ename)
    resp2 = requests.get(url=detail_url_full, headers=headers)
    html = etree.HTML(resp2.content.decode("gbk"))
    tale =html.xpath('.//div[@class="pop-bd"]/p/text()')
    a=""
    for i in tale:
        a=a+i
    ws.append([hero_name,a])
wb.save('data/英雄故事.xlsx')

